import asyncio, pyautogui, random, os, smtplib
from core.base_actions import BaseActions
from core.utils import get_image_pos
from datetime import datetime
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.utils import formataddr

class CommonActions(BaseActions):
  
  
  
  async def screenshot(self, filename):
    current_date = datetime.now().strftime("%Y-%m-%d")  # 格式：2023-12-31
    await self.log(f"screenshot start")
    screenshot = pyautogui.screenshot()
    save_dir = os.path.join(os.getenv("SCREENSHOT_DIR"), current_date)
    os.makedirs(save_dir, exist_ok=True)  # 自动创建目录
    screenshot.save(os.path.join(save_dir, f"{filename}.webp"))
    await self.log(f"screenshot saved to {filename}.webp")
    await asyncio.sleep(random.uniform(1, 2))
  
  async def create_dh_object(self):
    await self.log("制造点火装置 start")
    pyautogui.press("num7")
    await asyncio.sleep(10)
    return
    dh_pos = get_image_pos("images_2/dian_A.png")
    create_count = 1
    while dh_pos is None:
      pyautogui.press("num7")
      await self.log(f"制造点火装置 {create_count}")
      create_count += 1
      await asyncio.sleep(10)
      dh_pos = get_image_pos("images_2/dian_A.png")
    await self.log("制造点火装置 end")
    await asyncio.sleep(random.uniform(1, 2))
  
  async def check_buffs(self, dh_buff=False, qs_buff=False, sl_buff=False):
    # 检查buffs
    await self.log("check_buffs start")
    pyautogui.press("num9" if random.random() > 0.5 else "2")
    await asyncio.sleep(random.uniform(2, 3))
    if dh_buff:
      dh_pos = get_image_pos("images_2/word_dh.png")
      if dh_pos:
        # await self.log("制造 点火")
        # pyautogui.press("num7" if random.random() > 0.5 else "v")
        # await asyncio.sleep(random.uniform(8, 10))
        await self.log("使用 点火")
        pyautogui.press("num8")
        await asyncio.sleep(random.uniform(4, 5))
    if qs_buff:
      qs_pos = get_image_pos("images_2/word_qs.png")
      if qs_pos:
        await self.log("使用 奇思合计")
        pyautogui.press("num6")
        await asyncio.sleep(random.uniform(1.5, 2))
    if sl_buff:
      await self.log("使用 碎裂精华")
      pyautogui.press("num5" if random.random() > 0.5 else "x")
      await asyncio.sleep(random.uniform(2.5, 3.5))
        
    await self.log("check_buffs end")
    
  async def open_email_frame(self):
    await self.log("open email frame start")
    await asyncio.sleep(random.uniform(1.5, 2))
    # 使用凯蒂玩具
    pyautogui.press("num4" if random.random() > 0.5 else "z")
    await asyncio.sleep(random.uniform(1.5, 2))
    # target 凯蒂
    pyautogui.press("num4" if random.random() > 0.5 else "z")
    await asyncio.sleep(random.uniform(1.5, 2))
    # 与凯蒂交互
    pyautogui.press("9")
    await asyncio.sleep(random.uniform(1.5, 2))
    # 打开邮箱界面
    pyautogui.press("num4" if random.random() > 0.5 else "z")
    await asyncio.sleep(random.uniform(2, 3))
    await self.log("open email frame end")
    
  async def get_emails(self, sleep_suffix_time=None):
    await self.open_email_frame()
    pyautogui.press("num3" if random.random() > 0.5 else "g")
    await asyncio.sleep(random.uniform(4, 5))
    await self.log("get emails end")

  async def reset_keymap(self):
    await self.log("重置按键")
    await asyncio.sleep(random.uniform(1, 2))
    pyautogui.press('f4')
    await asyncio.sleep(random.uniform(1, 2))
    await self.log("重置按键完成")

  async def toggle_dz_table(self):
    await self.log("开始切换锻造桌子")
    await asyncio.sleep(random.uniform(1, 1.5))
    pyautogui.press('8' if random.random() > 0.5 else "r")
    await asyncio.sleep(random.uniform(1, 1.5))
    pyautogui.press('8' if random.random() > 0.5 else "r")
    await asyncio.sleep(random.uniform(1, 1.5))
    pyautogui.press('9')
    await asyncio.sleep(random.uniform(1, 1.5))
    pyautogui.press('8' if random.random() > 0.5 else "r")
    await asyncio.sleep(random.uniform(1, 1.5))
    await self.log("切换锻造桌子完成")

  async def toggle_fm_table(self):
    await self.log("开始切换附魔桌子")
    await asyncio.sleep(random.uniform(1, 1.5))
    pyautogui.press('7' if random.random() > 0.5 else "q")
    await asyncio.sleep(random.uniform(1, 1.5))
    pyautogui.press('7' if random.random() > 0.5 else "q")
    await asyncio.sleep(random.uniform(1, 1.5))
    pyautogui.press('9')
    await asyncio.sleep(random.uniform(1, 1.5))
    pyautogui.press('7' if random.random() > 0.5 else "q")
    await asyncio.sleep(random.uniform(1, 1.5))
    await self.log("切换附魔桌子完成")
  
  async def open_dz_frame(self):
    await self.log("打开锻造界面 start")
    # 打开锻造界面
    pyautogui.press("num1" if random.random() > 0.5 else "5")
    await asyncio.sleep(random.uniform(2, 3))
    await self.log("打开锻造界面 end")

  async def open_fm_frame(self):
    await self.log("打开附魔界面 start")
    # 打开锻造界面
    pyautogui.press("6" if random.random() > 0.5 else "v")
    await asyncio.sleep(random.uniform(2, 3))
    await self.log("打开附魔界面 end")
    
  async def toggle_to_order_tab(self):
    await self.log("切换到订单选项卡 start")
    pyautogui.press("i")
    await self.log("切换到订单选项卡成功")
    await asyncio.sleep(random.uniform(2, 3))
    # await self.log("切换到订单选项卡 start")
    # order_tab_pos = get_image_pos("images_2/order_tab.png")
    # while order_tab_pos is None:
    #   await asyncio.sleep(1)
    #   order_tab_pos = get_image_pos("images_2/order_tab.png")
    # pyautogui.click(order_tab_pos)
    # await self.log("切换到订单选项卡成功")
    # await asyncio.sleep(random.uniform(2, 3))
    
  async def open_all_craft_regent_bag(self):
    open_count = 1
    await self.log("开始打开工匠商盟支出")
    box_pos = get_image_pos("images_2/jiang_F.png")
    while open_count < 20 and box_pos is None:
      pyautogui.press("u")
      await asyncio.sleep(random.uniform(0.3, 0.5))
      box_pos = get_image_pos("images_2/jiang_F.png")
      if box_pos is None:
        await self.log(f"开始打开工匠商盟支出{open_count}")
        pyautogui.press("1")
        await asyncio.sleep(random.uniform(0.8, 1.1))
        open_count += 1
      
    await self.log("完成打开工匠商盟支出")
  
  async def send_role_bag_objects(self, only_send_craft_regents=False):
    if only_send_craft_regents:
      await self.open_email_frame()
    else:
      await self.get_emails()
      await asyncio.sleep(random.uniform(5, 6))
      await self.open_all_craft_regent_bag()
    await self.log("发送未绑定物品")
    pyautogui.press("num2" if random.random() > 0.5 else "f")
    await asyncio.sleep(random.uniform(5, 6))
    await self.log("发送制造物品完成")
  
    
    
